﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_QC.DataService;
using WPF_QC.QA_ViewModel;



namespace WPF_QC.QA_View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowViewModel viewModel = new MainWindowViewModel();
        private List<UserControl> _CurrentFormsCollection = new List<UserControl>();

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += (s, e) => { this.DataContext = this.viewModel; };
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            Messenger.Default.Register<NavigationMenu>(this, OnNavigation);
            //Messenger.Default.Register<BusyMessage>(this, onBusy);
        }

        private void OnNavigation(NavigationMenu navigationMenu)
        {
            if (navigationMenu == null)
            {
                throw new SystemException();
            }
           /*
           if (navigationMessage.PageTitles.ToString() == "HomePage")
            {
                Messenger.Default.Send(new BusyMessage(true));
                var task = new Task(LongRunningTask);
                task.Start();
                return;
            }
           */ 
            NavigationDockPanel.Children.Clear();
            var CurrentForm = GetCurrentForm(navigationMenu.PageTitles);
            NavigationDockPanel.Children.Add(CurrentForm);
        }

        /*
        private void LongRunningTask()
        {
            Thread.Sleep(6000);
            Messenger.Default.Send(new BusyMessage(false));
        }
        */

        private UserControl GetCurrentForm(PageTitles pageTitle)
        {
            var UCName = pageTitle.ToString();
            var CurrentForm = _CurrentFormsCollection.Find(u => u.Name == UCName);
            if (CurrentForm == null)
            {
                switch (UCName)
                {
                    case "HomePage":
                        CurrentForm = new Home();
                        break;
                    case "MeasurementReports":
                        CurrentForm = new MeasurementReports();
                        break;
                    case "MeasurementEntry":
                        CurrentForm = new MeasurementEntry();
                        break;
                    case "RunningMachinesStatus":
                        CurrentForm = new RunningMachinesStatus();
                        break;

                }
                CurrentForm.Name = UCName;
            }
            return CurrentForm;
        }

    }
}
